﻿using System;
using System.IO;

namespace DNFImagePacks2Manager.Models
{
    public class BKItemHash(string name, string file, bool isWhole)
    {
        public string name { get; } = name;
        public string file { get; } = file;
        public bool isWhole { get; } = isWhole;

        public string GetAbsoluteFile(string bkPath)
        {
            return GetAbsoluteFile(file, bkPath);
        }

        public string GetRelativeFile(string bkPath, out bool isRelative) {
            string relativeFile = GetRelativeFile(file, bkPath, out bool relatived);
            isRelative = relatived;
            return relativeFile;
        }

        public static string GetAbsoluteFile(string file, string bkPath)
        {
            if (Path.IsPathRooted(file))
            {
                return file;
            }
            else
            {
                return Path.Combine(bkPath, file);
            }
        }

        public static string GetRelativeFile(string file, string bkPath, out bool isRelative)
        {
            isRelative = false;
            if (Path.IsPathRooted(file))
            {
                try
                {
                    string relativePath = Path.GetRelativePath(bkPath, file);
                    if (!Path.IsPathRooted(relativePath))
                    {
                        isRelative = true;
                        return relativePath;
                    }
                }
                catch (Exception)
                {
                }
                return file;
            }
            else
            {
                isRelative = true;
                return file;
            }
        }
    }
}
